﻿using System.Text;
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;

namespace IdeaNMR.Shared.Utils
{
    public static class Miscellaneous
    {
        /// <summary>
        /// Gets the List of all referenced assemblies with versions.
        /// </summary>
        /// <param name="ass">Assembly</param>
        /// <returns></returns>
        static public string GetAssembliesList(Assembly ass)
        {
            return GetAssembliesList(ass, null);
        }

        static public string GetAssembliesList(Assembly ass, Dictionary<string, string> assList)
        {
            bool stopRecursion = false;
            if (assList == null)
            {
                assList = new Dictionary<string, string>();
                stopRecursion = true;
            }
            AssemblyName asName = ass.GetName();
            try { assList.Add(asName.Name, asName.Version.ToString()); }
            catch { }
            foreach (AssemblyName refAssName in ass.GetReferencedAssemblies())
            {
                Assembly refAss = Assembly.Load(refAssName);
                try
                {
                    assList.Add(refAssName.Name, refAssName.Version.ToString());
                    // If this entry is already in the dictionary, the exception will be thrown and the recursion will not get executed.
                    // Otherwise a StackOverflow happens due to infinite recursion.
                    GetAssembliesList(refAss, assList);
                }
                catch { }
            }
            if (stopRecursion)
            {
                var asms = from l in assList
                           orderby l.Key
                           select new
                           {
                               Name = l.Key,
                               Version = l.Value
                           };
                int maxLength = assList.Keys.Max(k => k.Length); // Finds a assembly with longest name
                // Format string "{0, -x}{1}\n" where x is calculated from maxLength
                string formStr = "{0, -" + (maxLength + 4).ToString() + "}{1}\n";
                StringBuilder sb = new StringBuilder();
                foreach (var item in asms)
                {
                    sb.AppendFormat(formStr, item.Name, item.Version);
                }
                return sb.ToString();
            }
            return string.Empty;
        }

        static public string GetUniqueFileName(string userName)
        {
            StringBuilder finalResult = new StringBuilder();
            DateTime dt = DateTime.Now;
            finalResult.AppendFormat("_{0}{1}{2}{3}.{4}", dt.Hour.ToString(), dt.Minute.ToString(),
                dt.Month.ToString(), dt.Year.ToString(), userName.ToLower());
            return finalResult.ToString();
        }
    }

    public enum OSType
    {
        Windows,
        Unix,
        Mac
    }
}
